package com.qdevelop.model.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

public class DebugUtils {
	static DebugUtils dbu = new DebugUtils();
	public static DebugUtils getInstance(){
		return dbu;
	}
	
	/**
	 * 查看request信息
	 * @param request
	 */
	@SuppressWarnings("unchecked")
	public void debugRequest(HttpServletRequest request){
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		Enumeration paramNames = request.getParameterNames();
		String param;
		while (paramNames.hasMoreElements()) {
			param = (String) paramNames.nextElement();
			sb.append(param).append(":'").append(request.getParameter(param)).append("',");
		}
		sb.append("}");
		System.out.println(sb.toString());
	}
	
	@SuppressWarnings("unchecked")
	public void debugBean(Object bean){
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		try {		
			Class classType = bean.getClass();
			Field fieldlist[] = classType.getDeclaredFields(); 
			int len = fieldlist.length;
			Object value;
			for (int i = 0; i < len; i++) { 
				Field fld = fieldlist[i];
				String fieldName = fld.getName();
				sb.append(fieldName).append(":");
				String firstLetter = fieldName.substring(0, 1).toUpperCase();
				try{
					String getMethodName = "get" + firstLetter + fieldName.substring(1);
					Method getMethod = classType.getMethod(getMethodName, new Class[]{});
					value = getMethod.invoke(bean, new Object[]{});
					sb.append("\"").append(value.toString()).append("\"");
				}catch(Exception e){
					sb.append("null");
				}
				if(i<len-1){
					sb.append(",");
				}
			} 
		} catch (Exception e) {
			e.printStackTrace();
		} 
		sb.append("}");
//		return sb.toString();
		System.out.println(sb.toString());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
